//
//  AppDelegate+Configure.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2017/9/27.
//  Copyright © 2017年 筱鹏. All rights reserved.
//

import Foundation
import UIKit
import UserNotifications

extension AppDelegate {
    func requestLocalNotification() {
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.delegate = LocalNotification.shared
            center.getNotificationSettings(completionHandler: { (setStatus) in
                if setStatus.authorizationStatus == .notDetermined {
                    center.requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
                        fearfulPrint("\(granted)")
                    }
                } else if setStatus.authorizationStatus == .denied {
                    fearfulPrint("通知被拒绝")
                }
            })
        } else {
            let curSet = UIApplication.shared.currentUserNotificationSettings
            if curSet?.types.rawValue == 0 {
                if curSet?.categories?.count == 0 {
                    let notiCate = UIMutableUserNotificationCategory()
                    notiCate.identifier = "LocalNotification"
                    let notiAction = UIMutableUserNotificationAction()
                    notiAction.identifier = "LocalNotificationAction"
                    notiAction.activationMode = .background
                    notiAction.behavior = .default
                    notiAction.isAuthenticationRequired = true
                    notiAction.isDestructive = false
                    notiCate.setActions([notiAction], for: .default)
                    let notiSet = UIUserNotificationSettings(types: [.alert,.sound,.badge], categories: [notiCate])
                    UIApplication.shared.registerUserNotificationSettings(notiSet)
                } else {
                    fearfulPrint("通知被拒绝")
                }
            }
        }
    }
    
    func checkOpenBirthdayAlert() {
        BirthdayCalculateModel().openAllBirthdayAlert()
    }
}
